package com.jic.data.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.jic.data.model.vo.response.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.jic.data.model.entity.SaleSingleDetail;
import com.jic.data.mapper.SaleSingleDetailMapper;
import com.jic.data.service.SaleSingleDetailService;

import lombok.extern.slf4j.Slf4j;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class SaleSingleDetailServiceImpl implements SaleSingleDetailService {

    @Resource
    private SaleSingleDetailMapper mapper;


    /**
     * 商品销售-单品-90-销售单品明细
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<SaleSingleDetail> listSingleSaleDetail(SaleSingleDetail request) {
        List<SaleSingleDetail> list = mapper.selectBySelective(request);
         if(CollectionUtils.isNotEmpty(list)){
             list.forEach(item->{
                 item.setFormateSaleDate(DateUtil.format(item.getSaleDate(), "yyyy-MM-dd"));
             });
         }
//        List<SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>>> saleDetailResponseList = new ArrayList<>();
//        Map<Date, List<SaleSingleDetail>> groupbyDate = list.stream().collect(Collectors.groupingBy(SaleSingleDetail::getSaleDate));
//
//        groupbyDate.keySet().forEach(item -> {
//            SaleDetailResponse<ProductSaleDetailResponse<List<SaleCategoryDetailResponse>>> saleDetailResponse = new SaleDetailResponse();
//            saleDetailResponse.setDate(DateUtil.format(item, "yyyy-MM-dd"));
//
//            List<SaleSingleDetail> detailList = groupbyDate.get(item);
//            Map<String, List<SaleSingleDetail>> groupbyStore = detailList.stream().collect(Collectors.groupingBy(SaleSingleDetail::getStoreCode));
//
//            groupbyStore.keySet().forEach(temp -> {
//                ProductSaleDetailResponse<List<SaleCategoryDetailResponse>> productDetailResponse = new ProductSaleDetailResponse();
//
//                List<SaleSingleDetail> gyStore = groupbyStore.get(temp);
//                List<SaleCategoryDetailResponse> responseList = gyStore.stream().map(temp1 -> {
//                    SaleCategoryDetailResponse bo = new SaleCategoryDetailResponse();
//                    BeanUtils.copyProperties(temp1, bo);
//                    return bo;
//                }).filter(item1 -> item1 != null).collect(Collectors.toList());
//
//                productDetailResponse.setStoreCode(temp);
//                productDetailResponse.setProductList(responseList);
//
//                saleDetailResponse.getList().add(productDetailResponse);
//            });
//            saleDetailResponseList.add(saleDetailResponse);
//        });
        return list;
    }


    /**
     * 热销商品top10
     *
     * @param request
     * @return
     */
    @Override
    public HistogramChartResponse selectTopProduct(SaleSingleDetail request) {
        List<SaleSingleDetail> list = mapper.selectTopProduct(request);

        HistogramChartResponse response = new HistogramChartResponse();
        List<String> productName = new ArrayList<>();
        List<Double> saleAmount = new ArrayList<>();

        list.stream().forEach(item -> {
            productName.add(item.getProductName());
            saleAmount.add(item.getTotalSaleAmount().doubleValue());
        });

        response.setX(productName);
        response.setY1(saleAmount);

        return response;
    }


}
